package com.zhang.crawler.book;

import com.zhang.crawler.utils.JdbcUtil;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : zhang sq
 * @date : 2020/7/30 10:47
 **/
public class DoubanBook {

    public static String LOGIN_URL = "https://accounts.douban.com/passport/login";
    public static String USER_AGENT = "User-Agent";
    public static String USER_AGENT_VALUE = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0";


    public static void main(String[] args) throws Exception {

        //simulateLogin("17607183394", "zsq2170"); // 模拟登陆github的用户名和密码
        Map<String, String> cookies = new HashMap();
        cookies.put("loc-last-index-location-id", "118254");
        cookies.put("dbcl2", "164885001:pu3jCf6Fsls");


        String url = "https://book.douban.com/tag/?view=type&icn=index-sorttags-all";
        Elements els = Jsoup.connect(url)
                .header("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)")
                .cookies(cookies)
                .get().select(".tagCol").select("a");
        List<String> tagList = new ArrayList<>();
        int i=0;
        for (Element e : els) {
            i++;
            if(i>=45){
                tagList.add(e.text());
            }
            //System.out.println(e.text());
        }
        for (String tag : tagList) {
            execute1(tag);
            Thread.sleep(3000);
        }
    }

    public static void execute1(String tag) throws Exception {


        Map<String, String> cookies = new HashMap();
        cookies.put("loc-last-index-location-id", "118254");
        cookies.put("dbcl2", "164885001:pu3jCf6Fsls");

        for (int i = 0; i < 15000; i = i + 20) {
            String url = "https://book.douban.com/tag/" + tag + "?start=" + i + "&type=T";
            Elements els = Jsoup.connect(url)
                    .header("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)")
                    .cookies(cookies).get().select(".subject-item");
            if(els.size()==0){
                Thread.sleep(2000);
                break;
            }
            List<DoubanBookEntity> entityList = new ArrayList<>();
            DoubanBookEntity entity = null;
            for (Element e : els) {
                entity = new DoubanBookEntity();
                entity.setBookType(tag);
                Element a1 = e.select(".pic").select("a").get(0);
                String href = a1.select(".nbg").get(0).attr("href");//图书详情地址
                entity.setDetailUrl(href);
                String picUrl = a1.select("img").attr("src"); //图片地址
                entity.setPicUrl(picUrl);
                Element a2 = e.select(".info").get(0);
                String title = a2.select("h2").select("a").attr("title"); //图书名称
                entity.setTitle(title);
                String[] book = a2.select(".pub").get(0).text().split("/");
                if (book != null && book.length > 0) {
                    if(book.length==1){
                        String author = book[0];  //作者
                        entity.setAuthor(author);
                    }else if (book.length == 2) {
                        String author = book[0];  //作者
                        entity.setAuthor(author);
                        String press = book[1];   //出版社
                        entity.setPress(press);
                    } else if (book.length == 3) {
                        String author = book[0];  //作者
                        entity.setAuthor(author);
                        String press = book[1];   //出版社
                        entity.setPress(press);
                        String publication = book[2]; //出版时间
                        entity.setPublication(publication);
                    } else if (book.length == 4) {
                        String author = book[0];  //作者
                        entity.setAuthor(author);
                        String press = book[1];   //出版社
                        entity.setPress(press);
                        String publication = book[2]; //出版时间
                        entity.setPublication(publication);
                        String price = book[3];   //价格
                        entity.setPrice(price);
                    } else if (book.length == 5) {
                        String author = book[0];  //作者
                        entity.setAuthor(author);
                        String translator=book[1];//译者
                        entity.setTranslator(translator);
                        String press = book[2];   //出版社
                        entity.setPress(press);
                        String publication = book[3]; //出版时间
                        entity.setPublication(publication);
                        String price = book[4];   //价格
                        entity.setPrice(price);
                    }

                }
                String grade = a2.select(".rating_nums").text(); //评分
                entity.setGrade(grade);
                String gradeNum = a2.select(".pl").text(); //评价数量
                entity.setGradeNum(gradeNum);
                String introductory = a2.select("p").text(); //简介
                entity.setIntroductory(introductory);
                String eleUrl = a2.select(".ft").select(".ebook-link").select("a").attr("href"); //电子版地址
                entity.setEleUrl(eleUrl);
                entityList.add(entity);
            }
            JdbcUtil.insert2(entityList);
            Thread.sleep(2000);
        }
    }


    /**
     * @param userName 用户名
     * @param pwd 密码
     * @throws Exception
     */
    public static void simulateLogin(String userName, String pwd) throws Exception {

        /*
         * 第一次请求
         * grab login form page first
         * 获取登陆提交的表单信息，及修改其提交data数据（login，password）
         */
        // get the response, which we will post to the action URL(rs.cookies())
        Connection con = Jsoup.connect(LOGIN_URL);  // 获取connection
        con.header(USER_AGENT, USER_AGENT_VALUE);   // 配置模拟浏览器
        Connection.Response rs = con.execute();                // 获取响应
        Document d1 = Jsoup.parse(rs.body());       // 转换为Dom树

        System.out.println(d1);

        List<Element> eleList = d1.select(".account-form");  // 获取提交form表单，可以通过查看页面源码代码得知

        // 获取cooking和表单属性
        // lets make data map containing all the parameters and its values found in the form
        Map<String, String> datas = new HashMap<>();

        // 01/24/2019 17:45 bluetata 更新 -------------------------------------------------------------- Start ----------
        // GitHub多次改版更新，最新的提交request data为:

        // authenticity_token   ll0RJnG1f9XDAaN1DxnyTDzCs+YXweEZWel9kGkq8TvXH83HjCwPG048sJ/VVjDA94YmbF0qvUgcJx8/IKlP8Q==
        // commit  Sign+in
        // login   bluetata
        // password    password123
        // utf8    ✓

        for(int i = 0; i < eleList.size(); i++) {

            for (Element e : eleList.get(i).getAllElements()) {
                // 设置用户名
                if (e.attr("name").equals("login")) {
                    e.attr("value", userName);
                }
                // 设置用户密码
                if (e.attr("name").equals("password")) {
                    e.attr("value", pwd);
                }
                // 排除空值表单属性
                if (e.attr("name").length() > 0) {
                    datas.put(e.attr("name"), e.attr("value"));
                }
            }
        }


//      旧逻辑  delete  01/24/2019 17:49 bluetata --------------------------------------------start
//        for (Element e : eleList.get(0).getAllElements()) {
//            // 设置用户名
//            if (e.attr("name").equals("login")) {
//                e.attr("value", userName);
//            }
//            // 设置用户密码
//            if (e.attr("name").equals("password")) {
//                e.attr("value", pwd);
//            }
//            // 排除空值表单属性
//            if (e.attr("name").length() > 0) {
//                datas.put(e.attr("name"), e.attr("value"));
//            }
//        }
//      旧逻辑  delete  01/24/2019 17:49 bluetata --------------------------------------------end


        // 01/24/2019 17:45 bluetata 更新 --------------------------------------------------------------- End -----------

        /*
         * 第二次请求，以post方式提交表单数据以及cookie信息
         */
        Connection con2 = Jsoup.connect("https://book.douban.com/tag/?view=type&icn=index-sorttags-all");
        con2.header(USER_AGENT, USER_AGENT_VALUE);
        // 设置cookie和post上面的map数据
        Connection.Response login = con2.ignoreContentType(true).followRedirects(true).method(Connection.Method.POST).data(datas).cookies(rs.cookies()).execute();
        // 打印，登陆成功后的信息
        System.out.println(login.body());

        // 登陆成功后的cookie信息，可以保存到本地，以后登陆时，只需一次登陆即可
        Map<String, String> map = login.cookies();
        for (String s : map.keySet()) {
            System.out.println(s + " : " + map.get(s));
        }
    }

}
